# -*- coding: utf-8 -*-

from xadmin import model

class ItemDatabase(object):
    def create(self, item):
        model.Session.save(item)
        model.Session.commit()

    def delete(self, item):
        model.Session.delete(item)
        model.Session.commit()

    def get(self, cls, itemid):
        item = model.Session.query(cls).get(itemid)
        return item

    def update(self, item):
        model.Session.commit()

class AbstractMethod(Exception): pass

class AbstractAdministrator(object):
    def getAll(self):
        raise AbstractMethod()
    def getById(self):
        raise AbstractMethod()
    def create(self, item):
        raise AbstractMethod()
    def delete(self, itemId):
        raise AbstractMethod()
    def update(self, itemId, item):
        raise AbstractMethod()

class ResourceNotFoundException(Exception): pass

class ResourceAdministrator(AbstractAdministrator):

    def __init__(self, cls):
        self.db = ItemDatabase()
        self.cls = cls

    def getAll(self):
        return model.Session.query(self.cls).all()

    def getById(self, id):
        resource = self.db.get(self.cls, id)
        if resource is None:
            raise ResourceNotFoundException(id)
        return resource

    def delete(self, itemId):
        try:
            item = self.getById(itemId)
            self.db.delete(item)
        except ResourceNotFoundException, e:
            return

    def update(self, itemId, itemXML):
        item = self.getById(itemId)
        item.loadFromXML(itemXML)
        self.db.update(item)

    def create(self, itemXML):
        item = self.cls()
        item.loadFromXML(itemXML)
        self.db.create(item)
        return item.getId()

from sqlalchemy import exceptions
class UserAdministrator(ResourceAdministrator):
    def isValid(self, user, password):
        try:
            user = model.Session.query(self.cls).filter_by(username=user, password=password).one()
            return True
        except exceptions.InvalidRequestError, e:
            return False

from authkit.users import AuthKitError
from xadmin.lib.sqlalchemy_driver import UsersFromDatabase
from xadmin import model

class AdminErrorException(Exception): pass

class AuthKitUserAdministrator(ResourceAdministrator):

    def __init__(self):
        self._db = UsersFromDatabase(model)

    def getAll(self):
        return self._db.list_users()

    def getById(self): pass

    def create_group(self, group):
        try:
            self._db.group_create(group)
        except AuthKitError, e:
            raise AdminErrorException(e.message)

    def create(self, user):
        self._db.user_create(user.username, user.password, user.group)

    def create_role(self, role):
        try:
            self._db.role_create(role)
        except AuthKitError, e:
            raise AdminErrorException(e.message)

    def delete(self, user):
        self._db.user_delete(user)
